package com.travel.server.manager.impl.command.project;

import com.travel.common.CommonConstant;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.server.manager.dto.request.project.UpdateProjectRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.ProjectDal;
import com.travel.server.manager.impl.dal.ProjectDal.ProjectParam;
import com.travel.server.manager.impl.enums.ParentClassEnum;
import com.travel.server.manager.impl.enums.ShelfTypeEnum;
import com.travel.server.manager.model.ProjectInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/10/29 14:23
 * @Desc:
 */
@Component
@Slf4j
public class UpdateProjectCommand extends AbstractCommand<UpdateProjectRequest, EmptyResponse> {

    @Autowired
    private ProjectDal projectDal;

    @Override
    protected void checkInput(UpdateProjectRequest request) {
        if(StringUtils.isBlank(request.getIds())){
            throwInvalidParam();
        }
        if(request.getDown() == null && request.getParentClass() == null){
            throwInvalidParam();
        }
        if(request.getDown() != null && request.getParentClass() != null){
            throwInvalidParam();
        }
    }

    @Override
    public EmptyResponse handle(UpdateProjectRequest request) {
        checkInput(request);

        List<Integer> idList = CommonUtil.idsToList(request.getIds());

        ProjectParam param = new ProjectParam();
        param.setIdList(idList);
        List<ProjectInfo> projectInfoList = projectDal.selectByExample(param);

        if(CollectionUtil.isEmpty(projectInfoList)){
            return new EmptyResponse();
        }

        if(request.getDown() != null){
            Integer shelfType;
            if(CommonConstant.YES.equals(request.getDown())){
                shelfType = ShelfTypeEnum.RIGHT_NOW.getShelfType();
            }else if(CommonConstant.NO.equals(request.getDown())){
                shelfType = ShelfTypeEnum.NO_SALE.getShelfType();
            }else{
                throw new WorkException(WorkCode.INVALID_PARAM);
            }
            updateShelfType(projectInfoList, shelfType);
        }else{
            updateGroup(projectInfoList, request.getParentClass(), request.getNewValue());
        }
        return new EmptyResponse();
    }

    /**
     * 修改上下架状态
     * @param projectInfoList
     * @param shelfType
     */
    private void updateShelfType(List<ProjectInfo> projectInfoList, Integer shelfType){
        for(ProjectInfo projectInfo : projectInfoList){
            projectInfo.setShelfType(shelfType);
            projectDal.insertOrUpdate(projectInfo);
        }
    }

    private void updateGroup(List<ProjectInfo> projectInfoList, Integer parentClass , String newValue){
        for(ProjectInfo projectInfo : projectInfoList){
            if(ParentClassEnum.PROJECT.getParentClass().equals(parentClass)){
                projectInfo.setProjectGroup(newValue);
            }else if(ParentClassEnum.PROJECT_TYPE.getParentClass().equals(parentClass)){
                projectInfo.setProjectType(newValue);
            }else if(ParentClassEnum.COUNTRY.getParentClass().equals(parentClass)){
                projectInfo.setCountry(newValue);
            }
            projectDal.insertOrUpdate(projectInfo);
        }
    }
}
